<?php
 	$ultKey = "Pn183ZSTalhlgGOcjAQ43L0d3ZPX9lh7r9sg6bjqGPEA4SVl4R1G1o43N25n7hJ235N1WMq8q9";
	
	function encrypt($string, $key) 
	{ 
		$result = ''; 
		for($i=0; $i<strlen($string); $i++) 
		{	
			$char = substr($string, $i, 1); 
			$keychar = substr($key, ($i % strlen($key))-1, 1); 
			$char = chr(ord($char)+ord($keychar)); 
			$result.=$char; 
		}

		return $result; 
	}
	function decrypt($string, $key) 
	{ 
		$result = ''; 

		for($i=0; $i<strlen($string); $i++) 
		{ 
			$char = substr($string, $i, 1); 
			$keychar = substr($key, ($i % strlen($key))-1, 1); 
			$char = chr(ord($char)-ord($keychar)); 
			$result.=$char; 
		}
		return $result; 
	}
		
	if(!isset($_SESSION['realpage']) || $_SESSION['realpage'] == false)
	{
		require './../Pages/hack.php';
		die('');
	}


	
	function contains($substring, $string) {
	        $pos = strpos($string, $substring);
	 
	        if($pos === false) {
	                // string needle NOT found in haystack
	                return false;
	        }
	        else {
	                // string needle found in haystack
	                return true;
	        }
	 
	}
	

	$dontshwfrm = ((!isset($_GET['method'])) || $_GET['method'] == null ) ? false: true;
	
	if($dontshwfrm)
	{
		if( !isset($_GET['dbaddr']) || $_GET['dbaddr'] == ""  ||
			!isset($_GET['dbname']) || $_GET['dbname'] == "" ||
			!isset($_GET['dbuser']) || $_GET['dbuser'] == "" )
			{
				$dontshwfrm = false;
				$error = "empty params!";
			}
		else 
		{
			$dbAddr = $_GET['dbaddr'];	
			$dbname = $_GET['dbname'];	
			$dbuser = trim($_GET['dbuser']);	
			$dbport = $_GET['dbport'];	
			if(isset($_GET['dbpassword']))
			{
				$dbpass = $_GET['dbpassword'];
			}
			else 
			{
				$dbpass = "";
			}
					
		}
	}
	
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Cp1252">
<LINK REL=StyleSheet HREF="./../CSS/MainCSS.css" TYPE="text/css" title="Main CSS file"/>
<title>Urban Dynamics - Installation page</title>
</head>


    <body>
    	<div class="hackTitle">
    	 Installation page
    	</div>    	
    	
	<br/>
	<div class = "stickLeft" style="width:26%"></div>
	<div class = "stickLeft" style="width:30%">
		
			<?php 
			if($dontshwfrm)
			{
				
				// 1. verify db connection, $error shoudl contain '' if successfull
				
				$urlwithport = $dbAddr;
				if (isset($dbport) && $dbport != '')
				{
					$urlwithport = $urlwithport . ':' . $dbport ;
				}
				
				$sqlLink = mysql_connect($urlwithport,$dbuser,$dbpass) or $error = mysql_errno();
				
				if ( !$sqlLink )
				{
					$error = "could not connect, error number ".$error;
				}
				else
				{
					//mysql_select_db($dbname,$sqlLink) or $error = mysql_errno();
				}
				
				if(isset($error) || $sqlLink == null)
				{ ?>
				
				<UL class="bsStream stickLeft">
					<li>
						SQL error, cannot connecto to db! check strings 
					</li>
					<li>	
						<?php echo $error;	?>
					</li>
					<li>	
						click here to <a href="./Install.php"> go back</a>
					</li>
				</UL>
				<?php 
				}
				else 
				{
					//first up, close connection
					mysql_close($sqlLink);
					
					// write new properties file!
					$myFile = "./../config/UrbanDynamics.properties";
					$fh = fopen($myFile, 'w') or $error="can't open file";
					
					if(!isset($error))
					{
						$stringData = encrypt ("DBURL=". $dbAddr,$ultKey)."\n" ; 
						fwrite($fh, $stringData);
						
						$stringData = encrypt ("DBPASS=". $dbpass,$ultKey)."\n" ; 
						fwrite($fh, $stringData);
						
						$stringData = encrypt ("DBUSER=". $dbuser,$ultKey)."\n" ; 
						fwrite($fh, $stringData);
						
						$stringData = encrypt ("DBPORT=". $dbport,$ultKey)."\n" ; 
						fwrite($fh, $stringData);
						
						$stringData = encrypt ("DBSCHEME=". $dbname,$ultKey)."\n" ; 
						fwrite($fh, $stringData);
						
						fclose($fh);
					}
				}
				
				// FINAL stage
				if(!isset($error))
				{
					require './../config/dbInstaller.php';
					
					if(!isset($error) || trim($error== ''))
					{ ?>
					<UL class="bsStream stickLeft">
						<li>
							Finished OK!
						</li>
						<li>	
							click here to <a href="./WelcomePage.php"> go back</a>
						</li>
						<li>	
							OR <br/>
							<script>
							function ajaxFillDb(){
								//do ajax
								if (window.XMLHttpRequest)
								  {// code for IE7+, Firefox, Chrome, Opera, Safari
								  xmlhttp=new XMLHttpRequest();
								  }
								else
								  {// code for IE6, IE5
								  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
								  }
								xmlhttp.onreadystatechange=function()
								  {
								  if (xmlhttp.readyState==4 && xmlhttp.status==200)
								    {
									    if(xmlhttp.responseText == 'false')
									    {
									    	alert('error!');		    	
									    }
									    else
									    {
									    	alert('db filled!');
									    	window.location="./WelcomePage.php";
									    }
									}
								  };
								xmlhttp.open("GET","./ajax.php?sid=<?php echo session_id(); ?>&method=fillDB",true);
								xmlhttp.send();
								return false;
							}
							
							</script>
							click here to <a onclick="ajaxFillDb();"> fill database</a>
						</li>
					</UL>
				<?php }	else { ?>
					<UL class="bsStream stickLeft">
						<li>
							error <?php echo $error ; ?>
						</li>
						<li>	
							click here to <a href="./WelcomePage.php"> go back</a>
						</li>
						
					</UL>
				<?php }
					
				}
				
				else
				{?>
					<UL class="bsStream stickLeft">
						<li>	
							<?php echo $error;	?>
						</li>
						<li>	
							click here to <a href="./Installer.php"> go back</a>
						</li>
					</UL>
				<?php
				}
			} // end of install, now show form
			else
			{?>
			<form action="./Install.php">
				<UL class="bsStream stickLeft">
					<li style="visibility:hidden">
						<input type="hidden" name="method" value="execute" />
					</li>
					<li>
						<div> DB address</div> <input type="text" name="dbaddr" value=""/>
					</li>
					<li>
						<div> DB port</div> <input type="text" name="dbport" value=""/>
					</li>
					<li>
						<div> DB name</div> <input type="text" name="dbname" value=""/>
					</li>
					<li>
						<div> DB user</div> <input type="text" name="dbuser" value=""/>
					</li>
					<li>
						<div> DB password</div> <input type="text" name="dbpassword" value=""/>
					</li>
					<li>
						<input type="submit" />
					</li>
					<?php 
					if(isset($error) && '' != $error)
					{
					?>
					<li>
						<div> error in previous try</div> <br/>
						<div><?php echo $error;?></div>
					</li>
					<?php 
					}
					?>
				</UL>
			</form>
			<?php 
			}
			?>
	</div>
	<div class = "installImg" >
		<img  alt="Installation" src="./../Images/InstallImg.jpg" />
	</div>
	
    </body>
</html>

<?php 
	$_SESSION['realpage'] = false;
?>